import $axios from "@/common/http.js";//请求实例
import { Message } from "element-ui";
import router from "@/router"
import store from '@/store'


/**
 * 根据商品id查找属于那一个分类
 * @param {*} id 
 * @returns 
 */
export const getCateinfo = async (id) => {
    // console.log(id);
    return await $axios.get('/cateinfo', { params: { id } })
}

/**
 * 获取分类列表
 * @returns 
 */
export const getCateList = async () => {
    let rst = await $axios.get('/catelist?istree=1')
    return rst

}

/**
 * 添加分类
 * @param {*} data 
 */
export const addCate = async (data) => {
    let rst = await $axios.post('/cateadd', data)
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
                store.dispatch('shoplist/getCateList')
            }
        })
    }
}

/**
 * 编辑分类
 * @param {*} data 
 */

export const editCate = async (data) => {
    let rst = await $axios.post('/cateedit', data)
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
                store.dispatch('shoplist/getCateList')
            }
        })
    }
}

/**
 *  删除 分类    id
 * @param {*} id 
 */
export const delCate = async (id) => {
    let rst = await $axios.post('/catedelete', { id })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                // router.back()
                store.dispatch('shoplist/getCateList')
            }
        })
    }
}